<?php
/**
 * @author tungck
 * @filesource chamcong.php
 * quan ly cham cong
*/

class Chamcong extends CI_Controller{
    
    function __construct(){
        
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('adminlayout');
        $this->adminlayout->setLayout("admin/layout");
        
        $userSession = $this->session->userdata('user');
        
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }else{
            if($userSession['useradmin'] != 1){
                redirect(base_url('nhanvien/event/list_event'));
            }
        }
    }
    
    function danh_sach_cham_cong(){
        $title = "Danh sách các bộ phận";
        $this->load->model("admin/staff_managers_model");
        $result = $this->staff_managers_model->getAll();
        $data['result'] = $result;
        $this->adminlayout->view("admin/danh_sach_cham_cong",$data ,$title);        
    }
    
    function get_danh_sach_cham_cong(){
        try{
        $data['filter'] = $_POST['MaHoSo'];
        $data['sort'] = $_GET["jtSorting"];
        $data['startIndex'] = $_GET["jtStartIndex"];
        $data['pageSize'] = $_GET["jtPageSize"];
        $this->load->model("admin/chamcong_model");
        $result = $this->chamcong_model->getList($data);
        $jTableResult = array();
		$jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $result['recordCount'];
		$jTableResult['Records'] = $result['query'];
		print json_encode($jTableResult);
        }catch(Exception $ex){
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Records'] = 'afhkahfkafhkahfakfhka';
            print json_encode($jTableResult);
        }
    }
    
    
    function them_cham_cong(){
        $data['NgayLam'] = $_POST['NgayLam'];
        $data['GioVao'] = $_POST['GioVao'];
        $data['GioVe'] = $_POST['GioVe'];
        $data['Cong'] = $_POST['Cong'];
        $data['MaHoSo'] = $_POST['MaHoSo'];
        $this->load->model("admin/chamcong_model");
        $result = $this->chamcong_model->them_cham_cong($data);
        $jTableResult = array();
		$jTableResult['Result'] = "OK";
		$jTableResult['Record'] = $result;
		print json_encode($jTableResult);
    }
    
    function xoa_cham_cong(){
        try{
        if(isset($_POST["MaChamCong"])){
            $id = $_POST["MaChamCong"];
            $this->load->model("admin/chamcong_model");
            $this->chamcong_model->xoa_cham_cong($id);
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
            
        }
        }catch(Exception $ex){
            $jTableResult = array();
	        $jTableResult['Result'] = "ERROR";
	           $jTableResult['Message'] = $ex->message();
    	       print json_encode($jTableResult);
        }
        
    }
    
    function update_cham_cong(){
        
        $data['MaChamCong'] = $_POST['MaChamCong'];
        $data['NgayLam'] = $_POST['NgayLam'];
        $data['GioVao'] = $_POST['GioVao'];
        $data['GioVe'] = $_POST['GioVe'];
        $data['Cong'] = $_POST['Cong'];
        $data['MaHoSo'] = $_POST['MaHoSo'];
        $this->load->model("admin/chamcong_model");
        $this->chamcong_model->thay_doi_cham_cong($data);
        //Return result to jTable
		$jTableResult = array();
		$jTableResult['Result'] = "OK";
		print json_encode($jTableResult);
    }
    
    function maHoSo(){
        $this->load->model("admin/staff_managers_model");
        $result = $this->staff_managers_model->getAll();
        $jTableResult = array();
        $rows = array();
        foreach($result as $row)
        {
            $rows[]=array("Value"=>$row->MaHoSo,"DisplayText"=>$row->MaHoSo);

        }

		$jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;
        print json_encode($jTableResult);
    }
    
    
    function addExcel(){
        $this->load->model("admin/luongthuong_model");
        $this->load->model("admin/chamcong_model");
        $filename=$_FILES["excel"]["tmp_name"]; 
        require_once (APPPATH."third_party/excel-reader/excel_reader2.php");   
        $uploaddir = APPPATH.'uploads/';
        $uploadfile = $uploaddir . basename($_FILES['excel']['name']);
        if (move_uploaded_file($_FILES['excel']['tmp_name'], $uploadfile)) {
            echo "File is valid, and was successfully uploaded.\n";
        } else {
            echo "Possible file upload attack!\n";
        }
        $filename = $uploadfile;
        $data = new Spreadsheet_Excel_Reader($filename,true,"UTF-8");  
        $rowsnum = $data->rowcount($sheet_index=0);  
        $colsnum =  $data->colcount($sheet_index=0);  
        for ($i=2;$i<=$rowsnum;$i++){
            $date = DateTime::createFromFormat('d/m/Y', $data->val($i,1));
            $data1['NgayLam'] = $date->format('Y-m-d');
            $data1['GioVao'] = $data->val($i,2);
            $data1['GioVe'] = $data->val($i,3);
            $data1['Cong'] = $data->val($i,4);
            $data1['MaHoSo'] = $data->val($i,5);
            $result = $this->chamcong_model->them_cham_cong($data1);
            $data3['Thang'] = $date->format('m');
            $data3['Nam'] = $date->format('Y');
            $data3['MaHoSo'] = $data1['MaHoSo'];
            $data2['Thang'] = $date->format('m');
            $data2['Nam'] = $date->format('Y');
            $data2['MaHoSo'] = $data1['MaHoSo'];    
            $check = $this->luongthuong_model->auto_add($data3);
            if(count($check) < 1){
                $data2['Luong'] = $data1['Cong']*20000;
                $this->luongthuong_model->them_luong_thuong($data2);
            }else{
                $data2['Luong'] = $check[0]->Luong + $data1['Cong']*20000;
                $data2['ID'] = $check[0]->ID;
                $this->luongthuong_model->thay_doi_luong_thuong($data2);
            }
            
        }
        redirect(base_url("admin/chamcong/danh_sach_cham_cong"));      

    }
    
    
}

?>